package lesson01;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Auther: xiaofei
 * @Date: 2021/6/28 - 06 - 28 - 14:16
 * @Description: lesson01
 * @version: 1.0
 */
//客户端
public class TCPClientDemo01 {
    //这是一个main方法，是程序的入口：
    public static void main(String[] args){
        //InetAddress serverIP = null;
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //创建一个Socket连接
            socket = new Socket(serverIP,port);
            //发送消息，IO流
            os = socket.getOutputStream();
            os.write("你好，欢迎学习Java".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
